package com.ruoyi.finance.service.impl;

import com.alibaba.fastjson.JSON;
import com.ruoyi.finance.domain.*;
import com.ruoyi.finance.domain.response.*;
import com.ruoyi.finance.enums.BusinessCostTypesEnum;
import com.ruoyi.finance.enums.CfContractEntity;
import com.ruoyi.finance.enums.ContractTypeEnum;
import com.ruoyi.finance.mapper.financeCheckMapper;
import com.ruoyi.finance.service.financeCheckService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @return name
 */
@Service
public class financeCheckServiceImpl implements financeCheckService {
    @Autowired
    private financeCheckMapper dao;

    @Override
    public List<JrRentInstallmentResponseDto> listAll(TJrquery tJrquery) {
        return dao.listAll(tJrquery);
    }

    @Override
    public JrRentInstallmentDetailsResponseDto selectInstallmentDetails(String id) {
        return dao.selectInstallmentDetails(id);
    }

    @Override
    public ResponseDetailCfContractDto detailCfContract(String cf_contract_id) {
        // 第一步 查询合同信息
        CfContractsEntity contractRecord = dao.selectSearchById(cf_contract_id);
        ResponseDetailCfContractDto resultDto = new ResponseDetailCfContractDto();
        BeanUtils.copyProperties(contractRecord, resultDto);
        // 第二步 查询收款计划表
        Map<String, Object> condition = new HashMap<String, Object>();
        // 合同ID
        condition.put("contract_id", cf_contract_id);
        // 合同类型：出房
        condition.put("contract_type", ContractTypeEnum.CF.getIndex());
        // 费用类型 ：租金
        condition.put("item_type", BusinessCostTypesEnum.ZUJIN.getIndex());
        List<CwReceiptPlanEntity> entityList = dao.selectByMap(condition);
        List<ReceiptDto> receiptList = new ArrayList<ReceiptDto>();
        ReceiptDto receiptDto = null;
        if (null != entityList && entityList.size() > 0) {
            for (CwReceiptPlanEntity record : entityList) {
                receiptDto = new ReceiptDto();
                receiptDto.setReceiptId(record.getId());
                BeanUtils.copyProperties(record, receiptDto);
                receiptList.add(receiptDto);
            }
        }
        resultDto.setReceiptList(receiptList);
        System.out.println("返回信息+++++++++++++++++++++++"+resultDto);
        return resultDto;
    }

    @Override
    public JrRentInstallmentResponseDto listshenhe(String id) {
        return dao.listshenhe(id);
    }

    @Override
    public boolean updateapply_state(String id,String apply_state) {
        return dao.updateapply_state(id,apply_state);
    }

    @Override
    public List<JrUrlResponseDto> selectAttachementUrlList(int index, long parseLong, int index1) {
        return dao.selectAttachementUrlList(index,parseLong,index1);
    }

    @Override
    public JrSfLoanDetailsResponseDto selectSfLoanDetails(String id) {
        JrSfLoanDetailsResponseDto selectSfLoanDetails = dao.selectSfLoanDetails(id);
        return selectSfLoanDetails;
    }

    @Override
    public ResponseDetailSfContractDto detailSfContract(String cf_contract_id) {
        // 查询收房合同信息
        SfContractEntity sfContractEntity = dao.selectSearchById2(cf_contract_id);
        ResponseDetailSfContractDto responseDetailSfContractDto = new ResponseDetailSfContractDto();
        BeanUtils.copyProperties(sfContractEntity, responseDetailSfContractDto);
        // 查询付款计划表
        HashMap<String, Object> hashMap = new HashMap<String, Object>();
        hashMap.put("contract_id", cf_contract_id);
        hashMap.put("contract_type", ContractTypeEnum.SF.getIndex());
        hashMap.put("item_type", BusinessCostTypesEnum.ZUJIN.getIndex());
        List<CwPaymentPlanEntity> entityList = dao.selectByMap2(hashMap);
        ArrayList<PaymentDto> paymentList = new ArrayList<PaymentDto>();
        PaymentDto paymentDto = null;
        if (null != entityList && entityList.size() > 0) {
            for (CwPaymentPlanEntity record : entityList) {
                paymentDto = new PaymentDto();
                BeanUtils.copyProperties(record, paymentDto);
                paymentDto.setPaymentId(record.getId());
                paymentList.add(paymentDto);
            }
        }
        // 续约时，结束时间加1
        Calendar c = Calendar.getInstance();
        Date leaseEndTime = responseDetailSfContractDto.getLeaseEndTime();
        c.setTime(leaseEndTime);
        c.add(Calendar.DATE, 1);
        leaseEndTime = c.getTime();
        responseDetailSfContractDto.setLeaseEndTime(leaseEndTime);
        responseDetailSfContractDto.setPaymentList(paymentList);
        return responseDetailSfContractDto;
    }

    @Override
    public List<JrFactoringDetailsResponseDto> selectFactoringDetails(String id) {
        return dao.selectFactoringDetails(id);
    }

    @Override
    public JrFactoringEntity selectFactoring(String id) {
        return dao.selectById(id);
    }

    @Override
    public ResponseDetailCompanyInfoDto detailCompanyInfo(Long businessId) {
        ResponseDetailCompanyInfoDto detailInfo = dao.detailCompanyInfo(businessId);
        if (detailInfo != null) {
            // 需还款金额
            detailInfo.setNeedPayBack(dao.getNeedPaybackMoney(businessId));
            // 查询账户绑定银行卡信息
            List<ResponseTMCompanyAccountDetailDto> accountDetailList = dao.getAccountDetailList(businessId);
            detailInfo.setAccountDetailList(accountDetailList);
        }
        return detailInfo;
    }

    @Override
    public ResponseUpdateBankInfoDto queryBankInfo(Long businessId) {
        return dao.queryBankInfo(businessId);
    }

}
